package com.cn.lyl.service.impl;

import com.cn.lyl.dao.UserDao;
import com.cn.lyl.dao.impl.UserDaoImpl;
import com.cn.lyl.model.UserModel;
import com.cn.lyl.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public String checkLogin(UserModel userModel) {

        if(userModel.getCode().equals(userModel.getVerityCode())){
            UserDao userDao = new UserDaoImpl();
            UserModel model = userDao.findByUsernameAndPassword(userModel);
            if(model == null)
            {
                return "error";
            }
            else{
                if (model.getEnable()==0){
                    return "lock";
                }
                return "success";
            }
        }
        return "error-code";
    }

    @Override
    public List<UserModel> findAllUser(UserModel userModel) {
        UserDao userDao = new UserDaoImpl();
        return  userDao.findAll(userModel);
    }

    @Override
    public String addUser(UserModel userModel) {
        UserDao userDao =new UserDaoImpl();
        return userDao.add(userModel)>0 ?"success":"error";
    }

    @Override
    public String deleteUser(UserModel userModel) {

        UserDao userDao = new UserDaoImpl();
        return userDao.delete(userModel)>0?"success":"error";
    }

    @Override
    public String changeInfo(UserModel userModel) {

        UserDao userDao = new UserDaoImpl();
        int flag=userDao.updateInfo(userModel);
        if(flag==1){
            return "success";
        }
        if (flag==2){
            return "repeat";
        }
        return "error";
    }

    @Override
    public String changePass(UserModel userModel) {
        return null;
    }

    @Override
    public UserModel findInfo(int id) {
        UserDao userDao = new UserDaoImpl();
        return userDao.findById(id);
    }

    @Override
    public String deleteUserReal(UserModel userModel) {
        UserDao userDao = new UserDaoImpl();
        return userDao.deleteUser(userModel)==1?"success":"error";
    }

    @Override
    public String changeEnable(UserModel userModel) {
        UserDao userDao = new UserDaoImpl();
        return userDao.updateEnable(userModel)>0?"success":"error";
    }

    @Override
    public int getCount(UserModel usermodel) {
        UserDao userDao = new UserDaoImpl();
        return userDao.countFindAll(usermodel);
    }
}
